import { htmlToDocx, convertHtmlToDocxBuffer, docxToBuffer } from '../src/index';

describe('HTML to DOCX Converter', () => {
  test('converts simple HTML to DOCX', async () => {
    const html = '<p>Hello World</p>';
    const result = await htmlToDocx(html);

    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with headings to DOCX', async () => {
    const html = `
      <h1>主标题</h1>
      <h2>副标题</h2>
      <p>这是一个段落。</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with text formatting', async () => {
    const html = `
      <p>这是<strong>粗体</strong>和<em>斜体</em>文本。</p>
      <p>这是<u>下划线</u>和<a href="#">链接</a>文本。</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with lists', async () => {
    const html = `
      <ul>
        <li>无序列表项 1</li>
        <li>无序列表项 2</li>
      </ul>
      <ol>
        <li>有序列表项 1</li>
        <li>有序列表项 2</li>
      </ol>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles unsupported tags with warnings', async () => {
    const html = `
      <p>正常段落</p>
      <video>不支持的视频标签</video>
      <canvas>不支持的 canvas 标签</canvas>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings.length).toBeGreaterThan(0);
    expect(result.warnings.some(warning => warning.includes('不支持的标签'))).toBe(true);
  });

  test('converts empty HTML', async () => {
    const html = '';
    const result = await htmlToDocx(html);

    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML to buffer directly', async () => {
    const html = '<p>测试文档</p>';
    const result = await convertHtmlToDocxBuffer(html);

    expect(result.buffer).toBeDefined();
    // 在 Node.js 环境下应该是 Buffer，在浏览器环境下应该是 Uint8Array
    expect(result.buffer instanceof Buffer || result.buffer instanceof Uint8Array).toBe(true);
    expect(result.buffer.length).toBeGreaterThan(0);
    expect(result.warnings).toEqual([]);
  });

  test('applies margin options correctly', async () => {
    const html = '<p>测试页边距</p>';
    const options = {
      margins: {
        top: 100,
        right: 80,
        bottom: 100,
        left: 80
      }
    };

    const result = await htmlToDocx(html, options);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts docx to buffer', async () => {
    const html = '<p>测试文档</p>';
    const { document } = await htmlToDocx(html);
    const buffer = await docxToBuffer(document);

    // 支持 Buffer (Node.js) 和 Uint8Array (Browser)
    expect(buffer instanceof Buffer || buffer instanceof Uint8Array).toBe(true);
    expect(buffer.length).toBeGreaterThan(0);
  });

  test('handles complex nested HTML', async () => {
    const html = `
      <div>
        <h1>文档标题</h1>
        <p>这是一个包含<strong>粗体</strong>和<em>斜体</em>的段落。</p>
        <div>
          <h2>子标题</h2>
          <ul>
            <li>列表项目 <strong>1</strong></li>
            <li>列表项目 <em>2</em></li>
          </ul>
        </div>
        <p>最后一个段落包含<a href="https://example.com">链接</a>。</p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with inline styles - colors', async () => {
    const html = `
      <h1 style="color: red;">红色标题</h1>
      <p style="color: #00FF00;">绿色段落</p>
      <p style="color: rgb(0,0,255);">蓝色RGB</p>
      <p style="color: rgba(255,128,0,0.8);">橙色RGBA</p>
      <div style="color: purple;">紫色div</div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with font styles', async () => {
    const html = `
      <h1 style="font-family: 'Arial'; font-size: 24pt;">Arial 24pt标题</h1>
      <p style="font-family: 'Times New Roman'; font-size: 14px;">Times 14px段落</p>
      <p style="font-weight: bold; font-style: italic;">粗体斜体段落</p>
      <div style="font-size: 2em;">2em大小文字</div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with indentation', async () => {
    const html = `
      <p style="text-indent: 2em;">2em缩进段落</p>
      <p style="margin-left: 40px;">40px左边距段落</p>
      <div style="padding-left: 1.5em;">1.5em左内边距div</div>
      <ul style="margin-left: 20pt;">
        <li>缩进列表项1</li>
        <li style="text-indent: 10px;">额外缩进列表项2</li>
      </ul>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with text decoration', async () => {
    const html = `
      <p style="text-decoration: underline;">下划线文本</p>
      <p>包含<u style="color: red;">红色下划线</u>的文本</p>
      <a href="#" style="text-decoration: underline; color: purple;">紫色下划线链接</a>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with special tags', async () => {
    const html = `
      <p>正常文本<small>小号文本</small>正常文本</p>
      <p>正常文本<big>大号文本</big>正常文本</p>
      <p>H<sub>2</sub>O 和 E=mc<sup>2</sup></p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts complex styled HTML', async () => {
    const html = `
      <h1 style="color: #2E86C1; font-family: 'Arial'; font-size: 24pt; text-indent: 1em;">
        复杂样式标题
      </h1>
      <p style="color: #34495E; font-size: 14pt; text-indent: 2em; font-family: 'Times New Roman';">
        这是一个包含<strong style="color: red; font-size: 16pt;">红色大号粗体</strong>和
        <em style="color: blue; font-style: italic; font-size: 12pt;">蓝色小号斜体</em>的复杂段落。
      </p>
      <ul style="margin-left: 30px; color: #7F8C8D;">
        <li style="color: #E74C3C; font-weight: bold;">红色粗体列表项</li>
        <li style="color: #27AE60; font-size: 18pt;">绿色大号列表项</li>
        <li style="font-family: 'Courier New'; text-decoration: underline;">
          Courier字体下划线列表项
        </li>
      </ul>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles mixed style inheritance', async () => {
    const html = `
      <div style="color: blue; font-size: 16pt;">
        <p>蓝色段落继承div样式</p>
        <p style="color: red;">红色段落覆盖颜色</p>
        <ul>
          <li>蓝色列表项继承div样式</li>
          <li style="font-weight: bold;">蓝色粗体列表项</li>
        </ul>
        <div style="font-family: 'Courier New';">
          <p>蓝色Courier字体段落（继承两层样式）</p>
        </div>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with data-scale attribute', async () => {
    const html = `
      <p>正常文字 <span data-scale="55">55%缩放文字</span> 正常文字</p>
      <p data-scale="120">整个段落120%缩放文字</p>
      <h1 data-scale="80" style="color: red;">80%缩放红色标题</h1>
      <div data-scale="150">
        <p>150%缩放的div中的段落</p>
        <strong>150%缩放的粗体文字</strong>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles data-scale with font-size combination', async () => {
    const html = `
      <p style="font-size: 20pt;" data-scale="75">20pt字体75%字符间距缩放</p>
      <span style="font-size: 16px;" data-scale="125">16px字体125%字符间距缩放</span>
      <div data-scale="90">
        <p style="font-size: 18pt;">18pt字体在90%字符间距缩放div中</p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles data-scale with various formats', async () => {
    const html = `
      <p data-scale="50">50%字符间距缩放</p>
      <p data-scale="100">100%字符间距缩放(正常间距)</p>
      <p data-scale="200">200%字符间距缩放</p>
      <p data-scale="75%">75%字符间距缩放(带百分号)</p>
      <p data-scale="1.5">无效缩放值</p>
      <p data-scale="0">无效缩放值(零)</p>
      <p data-scale="-50">无效缩放值(负数)</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles nested data-scale inheritance', async () => {
    const html = `
      <div data-scale="80" style="font-size: 20pt;">
        外层div 80%字符间距缩放
        <p>继承80%字符间距缩放的段落</p>
        <div data-scale="125">
          <p>嵌套div 125%字符间距缩放</p>
          <span data-scale="75">75%字符间距缩放span</span>
        </div>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles data-scale with lists', async () => {
    const html = `
      <ul data-scale="110">
        <li>110%字符间距缩放列表项</li>
        <li data-scale="90">90%字符间距缩放列表项</li>
        <li style="font-size: 16pt;" data-scale="125">16pt字体125%字符间距缩放列表项</li>
      </ul>
      <ol style="font-size: 14pt;" data-scale="85">
        <li>14pt字体85%字符间距缩放有序列表项</li>
        <li><strong data-scale="130">130%字符间距缩放粗体文字</strong></li>
      </ol>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles complex nested HTML with scale inheritance like MsoNormal', async () => {
    // 测试用户提供的复杂HTML结构
    const html = `
      <p class="MsoNormal" style="text-align: left;" data-scale="55">
        <span style="font-size: 53pt; color: #ff0000;">
          <span style="font-family: 方正小标宋简体;">贵州省人力资源和社会保障厅文</span>
          <span style="font-family: 方正小标宋简体;">件</span>
        </span>
      </p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);

    // 验证没有不支持标签的警告（因为都是支持的标签）
    expect(result.warnings.length).toBe(0);
  });

  test('verifies scale calculation in nested structure', async () => {
    // 验证字符间距缩放：外层55%缩放，内层53pt字体
    // 预期结果：字体大小保持53pt，但字符间距缩放为55%
    const html = `
      <div data-scale="55">
        <span style="font-size: 53pt; color: red;">测试文字</span>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with base64 images', async () => {
    // 使用一个简单的 1x1 透明 PNG 的 base64 数据
    const base64Image = '';

    const html = `
      <p>这是一段包含图片的文字：</p>
      <img src="${base64Image}" alt="测试图片" />
      <p>图片下方的文字。</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    // 可能会有图片处理的警告，但不应该有错误
    expect(result.warnings.every(warning => !warning.includes('无法加载图片'))).toBe(true);
  });

  test('handles invalid image src gracefully', async () => {
    const html = `
      <p>这是一段包含无效图片的文字：</p>
      <img src="invalid-image-url" alt="无效图片" />
      <img src="" alt="空图片地址" />
      <img alt="缺少src属性" />
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    // 应该有关于无效图片的警告
    expect(result.warnings.length).toBeGreaterThan(0);
    expect(result.warnings.some(warning => warning.includes('无法加载图片') || warning.includes('缺少 src 属性'))).toBe(true);
  });

  test('applies image styling and size limits', async () => {
    const base64Image = '';

    const html = `
      <div style="text-indent: 20px;">
        <img src="${base64Image}" alt="带缩进的图片" style="text-indent: 10px;" />
      </div>
    `;

    const options = {
      maxImageWidth: 300,
      maxImageHeight: 200
    };

    const result = await htmlToDocx(html, options);
    expect(result.document).toBeDefined();
    expect(result.warnings.length).toBe(0);
  });

  test('handles images in Node.js environment limitations', async () => {
    // 测试远程图片在 Node.js 环境下的行为
    const html = `
      <img src="https://example.com/image.jpg" alt="远程图片" />
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    // 在 Node.js 环境下，远程图片应该产生警告
    expect(result.warnings.some(warning => warning.includes('Node.js 环境下仅支持 base64 图片')
      || warning.includes('无法加载图片'))).toBe(true);
  });

  test('handles blob URL images in Node.js environment', async () => {
    // 测试 blob URL 图片在 Node.js 环境下的行为
    const html = `
      <p>包含 blob URL 图片的段落：</p>
      <img src="blob:http://develop.wsb360.com:8099/0c1dea63-6545-4a17-8f1d-e0cc3010bf0b" alt="Blob图片" />
      <img src="blob:https://example.com/12345678-1234-5678-9abc-123456789abc" alt="另一个Blob图片" />
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    // 在 Node.js 环境下，blob URL 应该产生特定的警告
    expect(result.warnings.length).toBeGreaterThan(0);
    expect(result.warnings.some(warning => warning.includes('Node.js 环境下不支持 blob: URL')
      || warning.includes('无法加载图片'))).toBe(true);
  });

  test('handles mixed image sources with blob URLs', async () => {
    // 简化测试 - 直接测试blob URL图片
    const html = `
      <p>测试 Blob URL 图片:</p>
      <img src="blob:http://localhost:3000/abc123" alt="Blob图片" />
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();

    // 应该有关于 blob URL 的警告
    expect(result.warnings.length).toBeGreaterThan(0);
    // 验证包含了 blob URL 相关的警告
    const warningText = result.warnings.join(' ');
    expect(
      warningText.includes('blob:')
      || warningText.includes('无法加载图片')
    ).toBe(true);
  });

  test('handles different blob URL formats', async () => {
    // 简化测试 - 只测试单个blob URL
    const html = `
      <p>测试单个 Blob URL:</p>
      <img src="blob:http://develop.wsb360.com:8099/0c1dea63-6545-4a17-8f1d-e0cc3010bf0b" alt="开发环境Blob" />
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();

    // 在 Node.js 环境下，blob URL 会产生警告
    expect(result.warnings.length).toBeGreaterThan(0);
    expect(result.warnings.some(warning => warning.includes('blob:')
      || warning.includes('Node.js 环境下不支持 blob: URL')
      || warning.includes('无法加载图片'))).toBe(true);
  });

  test('converts HTML with text alignment styles', async () => {
    const html = `
      <h1 style="text-align: center;">居中标题</h1>
      <p style="text-align: left;">左对齐段落</p>
      <p style="text-align: right;">右对齐段落</p>
      <p style="text-align: justify;">两端对齐段落，这是一段较长的文字用来演示两端对齐的效果。</p>
      <div style="text-align: center;">
        <p>居中div中的段落</p>
        <span style="text-align: left;">左对齐span（应该继承div的居中对齐）</span>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles text alignment with other styles', async () => {
    const html = `
      <h2 style="text-align: center; color: red; font-size: 20pt;">居中红色标题</h2>
      <p style="text-align: justify; font-family: Arial; font-size: 14pt; text-indent: 2em;">
        两端对齐段落，包含多种样式：字体、大小、缩进等。这是一段较长的文字用来演示样式组合效果。
      </p>
      <ul style="text-align: center;">
        <li>居中对齐的列表项1</li>
        <li style="text-align: right;">右对齐的列表项2</li>
        <li>居中对齐的列表项3</li>
      </ul>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles invalid text alignment values', async () => {
    const html = `
      <p style="text-align: invalid;">无效对齐方式</p>
      <p style="text-align: top;">不支持的对齐方式</p>
      <p style="text-align: middle;">另一个不支持的对齐方式</p>
      <p style="text-align: ;">空的对齐方式</p>
      <p>无对齐方式的普通段落</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles nested text alignment inheritance', async () => {
    const html = `
      <div style="text-align: center; color: blue;">
        居中对齐的div文字
        <p>继承居中对齐的段落</p>
        <div style="text-align: right;">
          <p>嵌套右对齐div中的段落</p>
          <span>嵌套右对齐div中的span</span>
        </div>
        <p style="text-align: left;">明确左对齐的段落（覆盖继承的居中对齐）</p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles text alignment with data-scale combination', async () => {
    const html = `
      <h1 style="text-align: center; color: #FF6B35;" data-scale="80">80%字符间距居中标题</h1>
      <p style="text-align: justify; font-size: 16pt;" data-scale="110">
        两端对齐段落，110%字符间距。这是一段较长的文字，用来演示两端对齐和字符间距的组合效果。
      </p>
      <div style="text-align: right;" data-scale="90">
        <p style="font-size: 14pt;">右对齐div中的段落，90%字符间距</p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with line-height styles', async () => {
    const html = `
      <h1 style="line-height: 1.5;">1.5倍行高标题</h1>
      <p style="line-height: 2;">2倍行高段落</p>
      <p style="line-height: 24pt;">24pt行高段落</p>
      <p style="line-height: 30px;">30px行高段落</p>
      <div style="line-height: normal;">
        <p>normal行高div中的段落</p>
        <span>normal行高span</span>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles line-height with various units', async () => {
    const html = `
      <p style="line-height: 1;">1倍行高（数字）</p>
      <p style="line-height: 1.2;">1.2倍行高（小数）</p>
      <p style="line-height: 18pt;">18pt行高</p>
      <p style="line-height: 24px;">24px行高</p>
      <p style="line-height: 1.5em; font-size: 16pt;">1.5em行高，基于16pt字体</p>
      <p style="line-height: 150%;">150%行高</p>
      <p style="line-height: normal;">normal行高</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles line-height with other styles', async () => {
    const html = `
      <h2 style="line-height: 1.8; color: red; font-size: 20pt; text-align: center;">
        居中红色标题，1.8倍行高
      </h2>
      <p style="line-height: 2.5; font-family: Arial; font-size: 14pt; text-indent: 2em; color: blue;">
        蓝色段落，2.5倍行高，包含多种样式：字体、大小、缩进、颜色。
      </p>
      <ul style="line-height: 1.6;">
        <li>1.6倍行高的列表项1</li>
        <li style="line-height: 2.0;">2.0倍行高的列表项2</li>
        <li>1.6倍行高的列表项3</li>
      </ul>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles invalid line-height values', async () => {
    const html = `
      <p style="line-height: invalid;">无效行高值</p>
      <p style="line-height: -1;">负数行高值</p>
      <p style="line-height: 0;">零行高值</p>
      <p style="line-height: abc;">字母行高值</p>
      <p style="line-height: ;">空的行高值</p>
      <p>无行高样式的普通段落</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles nested line-height inheritance', async () => {
    const html = `
      <div style="line-height: 1.5; font-size: 18pt;">
        外层div，1.5倍行高，18pt字体
        <p>继承1.5倍行高的段落</p>
        <div style="line-height: 2.0;">
          <p>嵌套div，2.0倍行高的段落</p>
          <span style="line-height: 1.2;">1.2倍行高span</span>
        </div>
        <p style="line-height: normal;">normal行高段落（覆盖继承的1.5倍行高）</p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles line-height with font-size and data-scale combination', async () => {
    const html = `
      <h1 style="line-height: 1.8; color: #FF6B35; font-size: 24pt;" data-scale="80">
        24pt字体，80%字符间距，1.8倍行高标题
      </h1>
      <p style="line-height: 2.2; font-size: 16pt; text-align: justify;" data-scale="110">
        16pt字体，110%字符间距，2.2倍行高段落。这是一段较长的文字，用来演示行高、字体大小和字符间距的组合效果。
      </p>
      <div style="line-height: 1.4; text-align: center;" data-scale="90">
        <p style="font-size: 14pt; color: blue;">
          居中对齐，蓝色，14pt字体，90%字符间距，1.4倍行高段落
        </p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with basic table', async () => {
    const html = `
      <table>
        <tr>
          <th>标题1</th>
          <th>标题2</th>
          <th>标题3</th>
        </tr>
        <tr>
          <td>数据1</td>
          <td>数据2</td>
          <td>数据3</td>
        </tr>
        <tr>
          <td>数据4</td>
          <td>数据5</td>
          <td>数据6</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('converts HTML with styled table', async () => {
    const html = `
      <table style="border: 1px solid black;">
        <thead>
          <tr>
            <th style="color: red; font-weight: bold;">红色标题</th>
            <th style="color: blue; font-size: 16pt;">蓝色大号标题</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td style="text-align: center;">居中对齐数据</td>
            <td style="font-style: italic;">斜体数据</td>
          </tr>
          <tr>
            <td style="color: green;" data-scale="75">绿色75%间距数据</td>
            <td style="line-height: 1.5;">1.5倍行高数据</td>
          </tr>
        </tbody>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles table with complex content', async () => {
    const html = `
      <table>
        <tr>
          <th>产品名称</th>
          <th>价格</th>
          <th>描述</th>
        </tr>
        <tr>
          <td><strong>笔记本电脑</strong></td>
          <td style="color: red; font-weight: bold;">¥8,999</td>
          <td>
            高性能办公笔记本，配备：
            <br/>• Intel i7处理器
            <br/>• 16GB内存
            <br/>• 512GB SSD硬盘
          </td>
        </tr>
        <tr>
          <td><em>无线鼠标</em></td>
          <td style="color: blue;">¥159</td>
          <td>
            <span style="font-size: 14pt; color: #333;">
              人体工程学设计，<u>2年质保</u>
            </span>
          </td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles invalid table structures gracefully', async () => {
    const html = `
      <table>
        <!-- 空表格 -->
      </table>
      <table>
        <tr>
          <!-- 空行 -->
        </tr>
      </table>
      <table>
        <tr>
          <td>有效数据</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings.length).toBeGreaterThan(0);
    // 应该有关于空表格或空行的警告
    expect(result.warnings.some(warning => warning.includes('表格中没有找到行')
      || warning.includes('没有找到单元格'))).toBe(true);
  });

  test('handles table with mixed styling and data-scale', async () => {
    const html = `
      <table data-scale="90">
        <tr>
          <th style="font-size: 18pt; text-align: center;">90%间距标题</th>
          <th style="color: #2980B9;">蓝色标题</th>
        </tr>
        <tr>
          <td style="font-size: 14pt;" data-scale="110">
            14pt字体，110%间距（覆盖表格的90%间距）
          </td>
          <td style="line-height: 1.8; text-align: right;">
            1.8倍行高，右对齐，继承表格90%间距
          </td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles table within other elements', async () => {
    const html = `
      <div style="text-align: center; color: blue;">
        <h2>数据统计表</h2>
        <p>以下是本月的销售数据：</p>
        <table>
          <tr>
            <th>月份</th>
            <th>销售额</th>
            <th>增长率</th>
          </tr>
          <tr>
            <td>1月</td>
            <td>¥100,000</td>
            <td style="color: green;">+15%</td>
          </tr>
          <tr>
            <td>2月</td>
            <td>¥120,000</td>
            <td style="color: green;">+20%</td>
          </tr>
        </table>
        <p style="font-size: 12pt; color: gray;">
          注：数据统计截至当月最后一天
        </p>
      </div>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles table background colors correctly', async () => {
    const html = `
      <table>
        <tr>
          <th>普通表头</th>
          <th style="background-color: #FFCCCC;">红色背景表头</th>
          <th style="background-color: blue;">蓝色背景表头</th>
        </tr>
        <tr>
          <td>普通单元格</td>
          <td style="background-color: #CCFFCC;">绿色背景单元格</td>
          <td style="background-color: rgba(255,255,0,0.5);">黄色背景单元格</td>
        </tr>
        <tr>
          <td>没有背景的单元格</td>
          <td style="background-color: transparent;">透明背景单元格</td>
          <td style="background-color: #F0F0F0;">浅灰色背景单元格</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
    // 验证没有自动添加背景的警告或错误
  });

  test('handles table border styles correctly', async () => {
    const html = `
      <table style="border: 2px solid red;">
        <tr>
          <th style="border: 1px dashed blue;">虚线蓝色边框表头</th>
          <th style="border-top: 3px solid green; border-bottom: 1px dotted purple;">复合边框表头</th>
        </tr>
        <tr>
          <td style="border: none;">无边框单元格</td>
          <td style="border-width: 2px; border-style: double; border-color: orange;">双线橙色边框</td>
        </tr>
        <tr>
          <td style="border-left: 4px solid black; border-right: 2px dashed gray;">左右不同边框</td>
          <td style="border: 1px solid #333333;">深灰色边框</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles various border width units', async () => {
    const html = `
      <table>
        <tr>
          <td style="border: 1px solid red;">1px边框</td>
          <td style="border: 2pt solid blue;">2pt边框</td>
          <td style="border: thin solid green;">thin边框</td>
          <td style="border: medium solid orange;">medium边框</td>
        </tr>
        <tr>
          <td style="border: thick solid purple;">thick边框</td>
          <td style="border-width: 0.5em; border-style: solid; border-color: brown;">0.5em边框</td>
          <td style="border: 0 none;">零宽度边框</td>
          <td>默认边框</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles border inheritance and override', async () => {
    const html = `
      <table style="border: 1px solid black;">
        <tr style="border: 2px dashed red;">
          <td>继承行边框</td>
          <td style="border: 3px dotted blue;">覆盖单元格边框</td>
        </tr>
        <tr>
          <td>继承表格边框</td>
          <td style="border-top: 4px double green;">仅上边框不同</td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles complex border combinations', async () => {
    const html = `
      <table>
        <tr>
          <th style="border: 2px solid #2980B9; background-color: #EBF3FD;">
            蓝色边框+背景的表头
          </th>
          <th style="border-left: 3px solid red; border-right: 3px solid red; text-align: center;">
            左右红色边框居中表头
          </th>
        </tr>
        <tr>
          <td style="border: 1px dashed #27AE60; font-size: 14pt; color: green;">
            绿色虚线边框+样式单元格
          </td>
          <td style="border-top: 2px double purple; border-bottom: 2px double purple; line-height: 1.5;">
            上下双线边框+行高单元格
          </td>
        </tr>
      </table>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles paragraph border styles correctly', async () => {
    const html = `
      <p style="border: 1px solid red;">1px红色实线边框段落</p>
      <p style="border: 2pt dashed blue;">2pt蓝色虚线边框段落</p>
      <p style="border-top: 3px solid green; border-bottom: 1px dotted purple;">复合边框段落</p>
      <p style="border: none;">无边框段落</p>
      <p style="border-width: 2px; border-style: double; border-color: orange;">双线橙色边框段落</p>
      <p style="border-left: 4px solid black; border-right: 2px dashed gray;">左右不同边框段落</p>
      <p style="border: 1px solid #333333; text-align: center; padding: 10px;">居中对齐带内边距的深灰色边框段落</p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
  });

  test('handles CSS inheritance correctly', async () => {
    const html = `
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS继承测试</title>
    <style>
        body {
            color: red;
            font-size: 16px;
            font-family: "微软雅黑";
        }
        
        div {
            margin: 10px;
        }
    </style>
</head>
<body>
    <div id="aaa">
        打火机手动滑稽阿萨德黄金卡
    </div>
    <div>
        这是另一个div，应该继承body的红色
    </div>
    <p>
        这是一个段落，也应该继承红色
    </p>
</body>
</html>`;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
    console.log('CSS继承测试完成，文档已生成');
  });

  test('handles data-scale attribute correctly', async () => {
    const html = `
      <p class="MsoNormal" style="text-align: left; transform: scaleX(0.55); transform-origin: left center; white-space: nowrap; line-height: 1;" data-scale="55" data-mce-style="text-align: left; transform: scaleX(0.55); transform-origin: left center; white-space: nowrap; line-height: 1;">
        <span style="font-size: 53pt; color: #ff0000;" data-mce-style="font-size: 53pt; color: #ff0000;">
          <span style="font-family: 方正小标宋简体;" data-mce-style="font-family: 方正小标宋简体;">贵州省人力资源和社会保障厅文</span>
          <span style="font-family: 方正小标宋简体;" data-mce-style="font-family: 方正小标宋简体;">件</span>
        </span>
      </p>
    `;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
    console.log('Data-scale测试完成，文档已生成');
  });

  test('handles complex data-scale with nested spans', async () => {
    const html = `
      <p class="MsoNormal" style="text-align: left; transform: scaleX(0.55); transform-origin: left center; white-space: nowrap; line-height: 1;" data-scale="55" data-mce-style="text-align: left; transform: scaleX(0.55); transform-origin: left center; white-space: nowrap; line-height: 1;">
        <span style="font-size: 53pt; color: #ff0000;" data-mce-style="font-size: 53pt; color: #ff0000;">
          <span style="font-family: 方正小标宋简体;" data-mce-style="font-family: 方正小标宋简体;">贵州省人力资源和社会保障厅文</span>
          <span style="font-family: 方正小标宋简体;" data-mce-style="font-family: 方正小标宋简体;">件</span>
        </span>
      </p>`;

    const result = await htmlToDocx(html);
    expect(result.document).toBeDefined();
    expect(result.warnings).toEqual([]);
    console.log('复杂data-scale测试完成，文档已生成');
  });
});
